import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket=null;//网络通信必须的
    public TcpEchoClient(String IP,int port) throws IOException {
        socket=new Socket(IP,port);//让客户端知到服务器的IP与端口号
    }
    public void start() throws IOException {//客户端启动程序
        System.out.println("-->");
        Scanner scanner=new Scanner(System.in);//通过键盘输入
        try(InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();) {
            Scanner scanner1=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);//包装成字符流好处理
            while (true){
                String request=scanner.next();//输入客户端请求
                printWriter.println(request);//把请求发送出去,并且带有换行符
                printWriter.flush();//刷新缓存区,使数据马上发送
                String response=scanner1.next();//读取服务器的响应
                System.out.printf("[%s:%d] req:%s reap:%s",socket.getInetAddress(),socket.getPort(),request,response);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient=new TcpEchoClient("10.79.177.79",9090);
        tcpEchoClient.start();
    }

}
